package cn.iocoder.yudao.module.cmt.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentArticleCategoryDO;
import cn.iocoder.yudao.module.cmt.service.content.ContentArticleCategoryService;

@Tag(name = "管理后台 - 文章分类")
@RestController
@RequestMapping("/cmt/content-article-category")
@Validated
public class ContentArticleCategoryController {

    @Resource
    private ContentArticleCategoryService articleCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建文章分类")
    @PreAuthorize("@ss.hasPermission('cmt:content-article-category:create')")
    public CommonResult<Long> createArticleCategory(@Valid @RequestBody ContentArticleCategorySaveReqVO createReqVO) {
        return success(articleCategoryService.createArticleCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章分类")
    @PreAuthorize("@ss.hasPermission('cmt:content-article-category:update')")
    public CommonResult<Boolean> updateArticleCategory(@Valid @RequestBody ContentArticleCategorySaveReqVO updateReqVO) {
        articleCategoryService.updateArticleCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:content-article-category:delete')")
    public CommonResult<Boolean> deleteArticleCategory(@RequestParam("id") Long id) {
        articleCategoryService.deleteArticleCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文章分类")
    @PreAuthorize("@ss.hasPermission('cmt:content-article-category:delete')")
    public CommonResult<Boolean> deleteArticleCategoryList(@RequestParam("ids") List<Long> ids) {
        articleCategoryService.deleteArticleCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:content-article-category:query')")
    public CommonResult<ContentArticleCategoryRespVO> getArticleCategory(@RequestParam("id") Long id) {
        ContentArticleCategoryDO articleCategory = articleCategoryService.getArticleCategory(id);
        return success(BeanUtils.toBean(articleCategory, ContentArticleCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章分类分页")
    @PreAuthorize("@ss.hasPermission('cmt:content-article-category:query')")
    public CommonResult<PageResult<ContentArticleCategoryRespVO>> getArticleCategoryPage(@Valid ContentArticleCategoryPageReqVO pageReqVO) {
        PageResult<ContentArticleCategoryDO> pageResult = articleCategoryService.getArticleCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContentArticleCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章分类 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:content-article-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleCategoryExcel(@Valid ContentArticleCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentArticleCategoryDO> list = articleCategoryService.getArticleCategoryPage(pageReqVO).getList();
        ExcelUtils.write(response, "文章分类.xls", "数据", ContentArticleCategoryRespVO.class,
                        BeanUtils.toBean(list, ContentArticleCategoryRespVO.class));
    }

}
